package cn.org.xiaosheng.context;

import cn.org.xiaosheng.handle.InspectionSolver;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XiaoSheng
 * @date 2025/3/17 下午8:53
 */
@Component
public class InspectionSolverChooser implements ApplicationContextAware {

    private Map<Integer, InspectionSolver> chooseMap = new HashMap<>();

    public InspectionSolver choose(int type) {
        return chooseMap.get(type);
    }

    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context=applicationContext;
    }

    @PostConstruct
    public void register() {
        Map<String, InspectionSolver> solverMap = context.getBeansOfType(InspectionSolver.class);
        for (InspectionSolver solver : solverMap.values()) {
            chooseMap.put(solver.supports(), solver);
        }
    }
}
